package com.atguigu.guli.serviceedu.service.impl;

import com.atguigu.guli.serviceedu.entity.Video;
import com.atguigu.guli.serviceedu.entity.vo.VideoVo;
import com.atguigu.guli.serviceedu.mapper.VideoMapper;
import com.atguigu.guli.serviceedu.service.VideoService;
import com.atguigu.servicebase.exception.Assert;
import com.atguigu.servicebase.exception.ServiceException;
import com.atguigu.servicebase.result.ResponseEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-02
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Override
    public boolean getCountByChapterId(String chapterId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        Integer count = baseMapper.selectCount(queryWrapper);
        return null != count && count > 0;
    }

    @Override
    public void saveVideoInfo(VideoVo videoVo) {
        Video video = new Video();
        videoVo.setId(null);
        BeanUtils.copyProperties(videoVo, video);
        boolean result = this.save(video);
        if(!result){
            throw new ServiceException(ResponseEnum.SAVE_VIDEOINFO_ERROR);
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        Integer count = baseMapper.delete(queryWrapper);
        Assert.notNull(count, ResponseEnum.DELETE_VIDEOINFO_ERROR);
    }
}
